/* ----------------------------------------------------------------------------------------------------------------
 * TextRenderer
 * ----------------------------------------------------------------------------------------------------------------
 */
package pure.engine.renderer 
{
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormatAlign;
	import flash.text.TextFormat;
	import pure.engine.system.ns_despair;
	
	use namespace ns_despair;
	
	/**
	 * 文本渲染器
	 * 
	 * @author Pure
	 * 
	 */
public class TextRenderer extends Renderer implements IText
{
	public function TextRenderer(width:Number,
								height:Number,
								text:String, 
								offsetX:Number = 0, offsetY:Number = 0) 
	{
		
		super();
		
		_buffer = new BitmapData(width, height, true, 0);

		m_x = offsetX - width * 0.5;
		m_y = offsetY - height * 0.5;
		m_range = (width > height?width:height) * 0.5;
		
		var temp:Number = Math.sqrt(width * width + height * height);
		//_localRect = new Rectangle( -temp * 0.5, -temp * 0.5, temp, temp );
		
		_textField = new TextField();
		//_textField.antiAliasType = AntiAliasType.ADVANCED;
		//_textField.gridFitType = GridFitType.PIXEL;
		//_textField.sharpness = -244;
		_textField.width = width;
		_textField.height = height;
		_textFormat = new TextFormat();
		_textFormat.align = TextFormatAlign.CENTER;
		_textField.defaultTextFormat = _textFormat;
		
		this.text = text;
		m_rangeDirty = true;
	}
	

	//======================
	// Member
	//======================

	protected var _container:Sprite;
	
	protected var _textField:TextField;
	
	protected var _textFormat:TextFormat;
	
	protected var _background:Boolean, _textDirty:Boolean;
	
	protected var _buffer:BitmapData;
	
	
	

	
	
	/**
	 * 设置背景
	 * 
	 * @param fillColor
	 * @param fillAlpha
	 * @param ellipse
	 * @param lineThickness
	 * @param lineColor
	 * @param lineAlpha
	 */
	public function setBackground(fillColor:uint,
								  fillAlpha:Number = 0.5, 
								  ellipse:Number = 0,
								  lineThickness:Number = 0,
								  lineColor:uint = 0,
								  lineAlpha:Number = 1):void
	{
		
		if (_container == null)
		{
			_container = new Sprite();
		}
		
		_container.graphics.beginFill(fillColor, fillAlpha);
		if (lineThickness > 0)
		{
			_container.graphics.lineStyle(lineThickness, lineColor, lineAlpha);
		}
		_container.graphics.drawRoundRect(lineThickness * 0.5, lineThickness * 0.5, _buffer.rect.width - lineThickness, _buffer.rect.height - lineThickness, ellipse, ellipse);
		
		_container.addChild(_textField);
		_background = true;
		_advanceTxt();
	}
	
	
	/**
	 * 大小
	 */
	public function get size():int{ return int(_textFormat.size); }
	public function set size(v:int):void 
	{
		if (this.size != v)
		{
			_textFormat.size = v;
			_textField.setTextFormat(_textFormat);
			_rejustVerticalAlign();
			_advanceTxt();
		}
	}
	
	
	/**
	 * 颜色
	 */
	public function get color():uint{ return uint(_textFormat.color); }
	public function set color(v:uint):void 
	{
		if (this.color != v)
		{
			_textFormat.color = v;
			_textField.setTextFormat(_textFormat);
			_advanceTxt();
		}
	}
	
	
	/**
	 * 粗细
	 */
	public function get bold():Boolean{ return Boolean(_textFormat.bold); }
	public function set bold(b:Boolean):void 
	{
		if (this.bold != b)
		{
			_textFormat.bold = b;
			_textField.setTextFormat(_textFormat);
			_advanceTxt();
		}
	}
	
	
	/**
	 * 字体
	 */
	public function get font():String{ return _textFormat.font; }
	public function set font(v:String):void 
	{
		if (this.font != v)
		{
			_textFormat.font = v;
			_textField.setTextFormat(_textFormat);
			_advanceTxt();
		}
	}
	
	
	/**
	 * 内容
	 */
	public function get text():String{ return _textField.text; }
	public function set text(v:String):void
	{
		if (_textField.text != v)
		{
			_textField.text = v;
			_rejustVerticalAlign();
			_advanceTxt();
		}
	}

	
	/**
	 * 文本滤镜
	 */
	public function get filters():Array { return _textField.filters; }
	public function set filters(value:Array):void
	{
		_textField.filters = value;
		_advanceTxt();
	}
	
	
	
	
	/* ----------------------------------------------------------------------------------------------------------------
	 * ★★★★★★★★★★★★★★★★★★★★★  private  ★★★★★★★★★★★★★★★★★★★★★
	 * ----------------------------------------------------------------------------------------------------------------
	 */
	
	
	 
	/**
	 * 重绘缓存
	 */
	protected function _redrawBuffer():void
	{
		if (_background)
		{
			_buffer.fillRect(_buffer.rect, 0x0);
			_buffer.draw(_container);
		}
		
		else
		{
			_buffer.draw(_textField);
		}
	}
	
	
	/**
	 * 调整垂直方向文本对齐
	 */
	protected function _rejustVerticalAlign():void
	{
		_textField.y = (_buffer.rect.height - _textField.textHeight - 4) >> 1;
	}
	
	
	protected function _advanceTxt():void
	{
		_textDirty = true;
		m_bufferDirty = true;
	}
	
	
	override protected function _generateBitmapData():BitmapData
	{
		if (_textDirty)
		{
			_redrawBuffer();
			_textDirty = false;
		}
		return _buffer;
	}
	
	
	override ns_despair function destroy():void
	{
		super.destroy();
		
		_buffer.dispose();
		_buffer = null;
		if (_container)
		{
			_container.removeChild(_textField);
			_container = null;
		}
		_textField = null;
		_textFormat = null;
		
	}
}

}